#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/1 15:29
# @Author  : huidong.bai
# @File    : tasks.py
# @Software: PyCharm
from celery_tasks.main import celery_app
import redis
from celery_tasks.speaker_monitor import speaker_monitor


@celery_app.task(name='celery_tasks.speaker_monitor.get_connect_speaker')
# 编写异步任务
def get_connect_speaker():
    """
    定时检测音箱在线设备，
    """
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='123456', db="1")
    con = redis.Redis(connection_pool=pool)
    pipeline = con.pipeline()

    # com_list = []
    # keys = con.keys()
    # for i in keys:
    #     key = i.decode()
    #     if "COM" in key:
    #         com_list.append(key)

    # 这里开始写查询设备列表
    device_infos = speaker_monitor.get_devices_list()

    # 更新redis数据库
    pipeline.flushdb()
    for item in device_infos:
        port = item.get('port')
        device_type = item.get('device_type')
        # sn = item.get('sn')
        # rom_version = item.get('rom_version')
        # detail = item.get('detail')

        if device_type is not None:
            pipeline.hset(port, 'device_type', device_type)
        # if sn is not None:
        #     pipeline.hset(port, 'sn', sn)
        # if rom_version is not None:
        #     pipeline.hset(port, 'rom_version', rom_version)
        # if detail is not None:
        #     pipeline.hset(port, 'detail', detail)

    pipeline.execute()
